
import 'package:baselib/utils/keyboard_util.dart';
import 'package:baselib/utils/load_util.dart';
import 'package:baselib/utils/md5_util.dart';
import 'package:baselib/utils/navigator_util.dart';
import 'package:baselib/utils/toast_util.dart';
import 'package:framelib/base/base_view_model.dart';
import 'package:framelib/config/api_config.dart';
import 'package:framelib/net/baseentity.dart';
import 'package:framelib/utils/string_util.dart';
import 'package:get/state_manager.dart';
import 'package:get/get.dart';
import 'package:module_business/module_comm/service/module_comm_service.dart';

class ForgetPasswordViewModel extends BaseViewModel<ModuleCommService>{

  String account = '';
  String password = '';
  String okpassword = '';
  //是否同意用户协议
  RxBool isAgreementSelected=true.obs;
  //是否登录按钮高亮
  RxBool isLoginFocus=false.obs;
  ModuleCommService moduleCommService;

  @override
  ModuleCommService createService() =>moduleCommService = ModuleCommService();

  @override
  void onInit() {
    //设置默认值
    // account = UserParamsModel.instance.phone;
    // password = UserParamsModel.instance.password;
    super.onInit();
    print('TestOneViewModel-->onInit');
    //Loading, Success, Error handle with 1 line of code
  }


  @override
  void onReady() {
    print('TestOneViewModel-->onReady');
    super.onReady();
  }

  @override
  void onClose() {
    print('TestOneViewModel-->onClose');
    super.onClose();
  }

  inputChanged(String value, int index) {
    switch(index){
      case 0:
        account=value?.trim();
        //监听输入框，便于控制登录按钮颜色
        break;
      case 1:
        password=value?.trim();
        break;
      case 2:
        okpassword=value?.trim();
        break;
    }
  }

  toFind() async{
    if(!(isValidate()))return;
    Map<String,dynamic> upData={
      'email':account,
      'password':Md5Util.generateMd5(
          Md5Util.generateMd5(okpassword + ApiConfig.wyyx_app_id)),
    };
    LoadUtil.showLoad();
    BaseEntity baseEntity = await moduleCommService.api_login_setPassWord(parmsMap: upData);
    KeyBoardUtil.downKeyBoard();
    LoadUtil.hideLoad();
    if (baseEntity?.code == ApiConfig.SUCCESS_CODE) {
      ToastUtil.toast(StringUtil.instance.Find_password_success);
      NavigatorUtil.goBack(result: {'email':account});
    } else {
      ToastUtil.toast(baseEntity?.message);
    }
  }

  //去用户协议页面
  toUserAgreement() async {
    // NavigatorUtil.pushByName(RouteConfig.commUrlPage,arguments: {
    //   'title':StringUtils.useprivacyagreement,
    //   'url':DrawableUtils.digtalts_privacy,
    //   'isAssets':true
    // });
  }

  selectAgreement() {
    isAgreementSelected.value=!isAgreementSelected.value;
  }

  isValidate() {
    if(account.isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_enter_your_account_number);
      return false;
    }
    if(password.isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_enter_a_password);
      return false;
    }
    if(okpassword.isEmpty){
      ToastUtil.toast(StringUtil.instance.Please_enter_the_password_again);
      return false;
    }
    if(password!=okpassword){
      ToastUtil.toast(StringUtil.instance.Please_enter_the_same_password);
      return false;
    }
    return true;
  }

  toTermsOfService() {}

  toPrivacyPolicy() {}

}
